from django.shortcuts import render, redirect
from course.models import course_manage
from course import page

def index(request):
    # 分页相关
    current_page = request.GET.get('page', 1)
    page_obj = page.Pager(current_page)

    info = course_manage.objects.all()
    # 获取course_manage的所有对象

    #尝试做成一个搜索框对应多个属性
    string = request.GET.get('course', '')

    if string.strip() != '':
        #判断搜索框是否存在值
        #问题二，课程代码存在首位有数字0的。。
        #判断courses.exists()是否与名字中的字段匹配，好像不用加了。。存在搜索但是没搜到东西默认就会什么都不展示。。是对的。
        if string.isdigit():
            #判断是否是纯数字,纯数字匹配id
            courses = course_manage.objects.filter(id__contains=string).order_by("-update_time")[page_obj.start:page_obj.end]

            all_item = course_manage.objects.filter(id__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/course/?course=' + string)


            # __contains表示包含。
            return render(request, 'course/index.html', {'courses': courses,'pager_str':pager_str,'info':info})
        else:
            courses = course_manage.objects.filter(course__contains=string).order_by("-update_time")[page_obj.start:page_obj.end]

            all_item = course_manage.objects.filter(course__contains=string).order_by("-update_time").count()
            pager_str = page_obj.page_str(all_item, '/course/?course=' + string)

            return render(request, 'course/index.html', {'courses': courses,'pager_str':pager_str,'info':info})
    else:#不存在则默认输出所有数据
        courses = course_manage.objects.all().order_by("-update_time")[page_obj.start:page_obj.end]

        all_item = course_manage.objects.all().order_by("-update_time").count()
        #print("数目%r"%all_item)
        pager_str2 = page_obj.page_str2(all_item, '/course/')

        return render(request, 'course/index.html', {'courses': courses,'pager_str':pager_str2,'info':info})

def add(request):
    if request.method == 'GET':
        return render(request, 'course/add.html')
    else:
        course = request.POST.get('course', '')
        teacher_id = request.POST.get('teacher_id', '')
        price = request.POST.get('price', '')
        price_bool = request.POST.get('price_bool', '')
        test_way = request.POST.get('test_way', '')
        video_time = request.POST.get('video_time', '')
        number = request.POST.get('number', '')

        test1 = course_manage(course=course, teacher_id=teacher_id, price=price, price_bool=price_bool,
                              test_way=test_way, video_time=video_time, number=number)  # 往输入库里面插入了一条，name=runoob，id自动生成。

        test1.save()
        return redirect('../')

# 教师信息修改处理函数
def edit(request):
    if request.method == 'GET':
        id = request.GET.get("id")
        course = course_manage.objects.get(id=id)

        return render(request, 'course/edit.html', {'course':course})
    else:
        id = request.POST.get('id')
        course = request.POST.get('course', '')
        teacher_id = request.POST.get('teacher_id', '')
        price = request.POST.get('price', '')
        price_bool = request.POST.get('price_bool', '')
        test_way = request.POST.get('test_way', '')
        video_time = request.POST.get('video_time', '')
        number = request.POST.get('number', '')


        course_manage.objects.filter(id=id).update(course=course, teacher_id=teacher_id, price=price,price_bool=price_bool,
                                                   test_way=test_way, video_time=video_time, number=number)
        return redirect('../')

def delete(request):
    id = request.GET.get("id")

    test1 = course_manage.objects.get(id=id)
    test1.delete()
    return redirect('../')